package com.tiyende.kash.loop.ui.activity

import android.annotation.SuppressLint
import android.view.View
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.base.app.TiyendeActivityStackManager
import com.tiyende.kash.loop.common.CommonActivity
import com.tiyende.kash.loop.databinding.TiyendeActivityApplyReviewingBinding
import com.tiyende.kash.loop.event.TiyendeEventData
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.utils.TiyendeTimerThread
import com.tiyende.kash.loop.utils.helper.TiyendeOrderHelper
import com.tiyende.kash.loop.utils.helper.TiyendeProcessHelper

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/25 18:45
 * @Annotation：风控审核中等待
 */
class TiyendeLoanReviewingActivity: CommonActivity() {
    lateinit var binding: TiyendeActivityApplyReviewingBinding
    //等待时长
    private var countdown = 0
    private var timerThread: TiyendeTimerThread? = null

    override fun getTiyendeRootView(): View {
        binding = TiyendeActivityApplyReviewingBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeBackBtnVisibility(View.INVISIBLE)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
        setTiyendeSwitchLangBtnRes(R.mipmap.tiyende_language_icon)
        setTiyendeTextTitleVisibility(View.GONE)
        setBackExit(true)
        eventRemark = "SMS_Preview_Page"
    }

    override fun initTiyendeListener() {
        super.initTiyendeListener()
    }

    override fun loadTiyendeData() {
        timerThread = TiyendeTimerThread(1000L).apply {
            setTimingListener(object : TiyendeTimerThread.ITimingThreadListener{
                override fun onLoading() {
                    countdown += 1
                    showUploadSmsDataTipsText()
                }
            })
        }
        timerThread?.startThread()
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showUploadSmsDataTipsText(){
        binding.applySmsDataCountdown.text = "$countdown"
        if (countdown % 5 == 0){
            //刷新主流程
            publishEventData(TiyendeEventData(TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESH))
        }
    }

    override fun onTiyendePublishEvent(event: TiyendeEventData) {
        super.onTiyendePublishEvent(event)
        when(event.eventType){
            TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESHED -> {
                hiddenTiyendeLoading()
                //风控审核中，
                if (TiyendeProcessHelper.queryTiyendeGuideLocalType() == "REVIEWING"){
                    return
                }
                //进入下载引导
                if (TiyendeProcessHelper.queryTiyendeGuideLocalType() == "GUIDE"){
                    //当前栈顶是 手机号校验页面 不重复跳转
                    if (!TiyendeActivityStackManager.isCurrentActivity(TiyendeLoanGuideActivity::class.java)){
                        onIntent(TiyendeLoanGuideActivity::class.java)
                    }
                } else {
                    //提交申请时的订单id 直接进入订单详情
                    if (TiyendeOrderHelper.queryOrderId.isNotEmpty()){
                        TiyendeOrderInfoActivity.start(this,TiyendeOrderHelper.queryOrderId)
                    }
                }
                timerThread?.release()
                finish()
            }
            else -> {

            }
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_LOAN_SMS_PREVIEW_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_LOAN_SMS_PREVIEW_END)
        timerThread?.release()
        timerThread = null
    }
}